import 'package:flutter/material.dart';

class IMKitRouter {
  late Map<String, WidgetBuilder> routes;

  IMKitRouter._() {
    routes = {};
  }

  static final IMKitRouter instance = IMKitRouter._();

  //全局的路由监听
  final RouteObserver<Route<dynamic>> routeObserver = RouteObserver();

  bool registerRouter(String name, WidgetBuilder builder, {Map<String, Object>? arguments, bool cover = false}) {
    if (routes[name] == null || cover) {
      routes[name] = builder;
      return true;
    }
    return false;
  }

  static T? getArgumentFormMap<T>(BuildContext context, String key) {
    var argument = ModalRoute.of(context)!.settings.arguments;
    if (argument == null || argument is! Map) {
      return null;
    }
    Map<String, Object?> argMap = argument as Map<String, Object?>;
    return argMap[key] as T?;
  }
}
